package com.wheesper.www.WheesperServer.Services;

import java.util.HashMap;
import java.util.Map;

import com.Wheesper.www.WheesperServer.Model.UserCenter;
import com.wheesper.www.WheesperServer.Net.Session.IOSession;
import com.wheesper.www.WheesperServer.Utils.CaptchaGenerator;
import com.wheesper.www.WheesperServer.Utils.Logger;

import ProtocolBuffer.ProtoMessageOuterClass.PasswordModifyCaptchaResponse;
import ProtocolBuffer.ProtoMessageOuterClass.PasswordModifyResponse;
import ProtocolBuffer.ProtoMessageOuterClass.ProtoMessage;

public class ModifyPasswordController {
	private Map<String,String> _email2captcha;
	
	private static ModifyPasswordController _instance = new ModifyPasswordController();
	public static ModifyPasswordController Instance() {
		return _instance;
	}
	
	public ModifyPasswordController() {
		this._email2captcha = new HashMap<String,String>();
	}
	
	private boolean checkCaptcha(String email,String captcha) {
		String cpha = _email2captcha.get(email);
		return (cpha != null) &&(cpha.contentEquals(captcha));
	}
	
	private void captchaSendedResponse(IOSession session,String email) {
		PasswordModifyCaptchaResponse response = PasswordModifyCaptchaResponse.newBuilder()
				.setMailAddress(email)
				.setStatus(true)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setPasswordModifyCaptchaResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
	}
	private void modifiedSuccessfullyResponse(IOSession session,String email) {
		PasswordModifyResponse response = PasswordModifyResponse.newBuilder()
				.setMailAddress(email)
				.setStatus(true)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setPasswordModifyResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
		_email2captcha.remove(email);
	}
	private void modifiedFailedResponse(IOSession session,String email) {
		PasswordModifyResponse response = PasswordModifyResponse.newBuilder()
				.setMailAddress(email)
				.setStatus(false)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setPasswordModifyResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
		_email2captcha.remove(email);
	}
	
	private void modifyRoutine(String email) {
		String captcha = CaptchaGenerator.genCatcha("mdfpswd" + email);
		_email2captcha.put(email, captcha);
	}
	
	public void modifyApply(IOSession session,String email) {
		modifyRoutine(email);
		//TODO:send Captcha
		captchaSendedResponse(session,email);
	}
	
	public void modifying(IOSession session,String email,String password,String captcha) {
		try {
			if(checkCaptcha(email,captcha)) {
				UserCenter.Instance().ModifyPassword(email, password);
				modifiedSuccessfullyResponse(session,email);
				Logger.Instance().logging("[Password Modify Sucessfully!]{\n"
						+ "Email:" + email + "\n"
						+ "NewPassword:" + password + "\n}");
			}else {
				modifiedFailedResponse(session,email);
				Logger.Instance().logging("[Password Modify Failed!]{\n"
						+ "Email:" + email + "\n"
						+ "Captcha:" + captcha + "\n"
						+ "Reason: Captcha Error! \n}");
			}
		}catch (Exception e) {
			Logger.Instance().logging(e.getMessage());
			modifiedFailedResponse(session,email);
			e.printStackTrace();
		}
	}
}
